<script>
	import Vue from 'vue'
	import {
		mapMutations
	} from 'vuex';
	import {
		websocketUrl,
		baseUrl
	} from "@/api/params";
	import {
		wechatH5Login
	} from "@/api/login"
	import {
		jieri
	} from "@/api/userInfo.js"
	export default {
		onLaunch() {
			uni.setStorageSync('fullscreenShow', true)
			// #ifdef APP-PLUS
			var server = baseUrl + "/tiny-shop/v1/site/app-update"; //检查更新地址

			uni.request({
				url: server,
				data: {
					appCurrentVer: plus.runtime.versionCode
				},
				method: 'POST',
				success: res => {
					console.log(res)
					if (res.statusCode == 200 && res.data.code == 200) {
						let data = res.data.data;
						if (plus.runtime.versionCode == data.current) {} else {
							uni.showLoading({
								title: '下载新版本，请稍等',
								mask: true
							});
							var dtask = plus.downloader.createDownload(data.download, {}, function(d, status) {
								// 下载完成
								if (status == 200) {
									plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
										uni.hideLoading()
										uni.showToast({
											title: '安装失败',
											mask: false,
											duration: 1500
										});
									})
								} else {
									uni.hideLoading()
									uni.showToast({
										title: '更新失败',
										mask: false,
										duration: 1500
									});
								}
							});
							dtask.start();

						}

					}
				}
			})


			// #endif


			uni.getSystemInfo({
				success(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.top - e.statusBarHeight;
					// #endif
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			});
			if (!uni.getStorageSync('accessToken')) {
				uni.removeTabBarBadge({
					index: 3
				});
			} else {
				if (uni.getStorageSync('cartNum') == 0) {
					uni.removeTabBarBadge({
						index: 3
					});
				} else {
					uni.setTabBarBadge({
						index: 3,
						text: uni.getStorageSync('cartNum').toString()
					});
				}
			}

			try {
				const value = uni.getStorageSync('storage');
				console.log(value);
				if (value) {
					console.log(value);
				}
			} catch (e) {
				console.log('error');
			}

			return
			if (this.isMobile()) {

			} else {
				// window.location = 'http://pc.dongfangqianmen.com/';
			}



		},
		onShow() {
			// 获取节日的信息
			this.$post(jieri).then(res => {
				console.log("res", res)
				uni.setStorageSync("jieri", res.data.is_change);
				// 0：关闭，1：春节，2：元旦，3：元宵，4：国庆，5：情人节，6：母亲节，7：父亲节，8：圣诞节
				if (res.data.is_change == 2) {
					// 这是元旦
					uni.setTabBarItem({
						index: 0,
						text: '首页',
						iconPath: '/static/yuandan/yuan1.png',
						selectedIconPath: '/static/yuandan/yuan1.png'
					})
					uni.setTabBarItem({
						index: 1,
						text: '分类',
						iconPath: '/static/yuandan/yuan2.png',
						selectedIconPath: '/static/yuandan/yuan2.png'
					})
					uni.setTabBarItem({
						index: 2,
						text: '直播',
						iconPath: '/static/yuandan/yuan3.png',
						selectedIconPath: '/static/yuandan/yuan3.png'
					})
					uni.setTabBarItem({
						index: 3,
						text: '购物车',
						iconPath: '/static/yuandan/yuan4.png',
						selectedIconPath: '/static/yuandan/yuan4.png'
					})
					uni.setTabBarItem({
						index: 4,
						text: '我的',
						iconPath: '/static/yuandan/yuan5.png',
						selectedIconPath: '/static/yuandan/yuan5.png'
					})
					uni.setTabBarStyle({
						color: '#FFFFFF',
						selectedColor: '#ffd479',
						backgroundColor: '#ed1717',
						borderStyle: 'white',
						backgroundImage: '/static/yuandan/yuanback.png',
						backgroundRepeat: "repeat"
					})
				}
				if (res.data.is_change == 8) {
					// 这是圣诞
					uni.setTabBarItem({
						index: 0,
						text: '首页',
						iconPath: '/static/shengdan/sheng1.png',
						selectedIconPath: '/static/shengdan/sheng1.png'
					})
					uni.setTabBarItem({
						index: 1,
						text: '分类',
						iconPath: '/static/shengdan/sheng2.png',
						selectedIconPath: '/static/shengdan/sheng2.png'
					})
					uni.setTabBarItem({
						index: 2,
						text: '直播',
						iconPath: '/static/shengdan/sheng3.png',
						selectedIconPath: '/static/shengdan/sheng3.png'
					})
					uni.setTabBarItem({
						index: 3,
						text: '购物车',
						iconPath: '/static/shengdan/sheng4.png',
						selectedIconPath: '/static/shengdan/sheng4.png'
					})
					uni.setTabBarItem({
						index: 4,
						text: '我的',
						iconPath: '/static/shengdan/sheng5.png',
						selectedIconPath: '/static/shengdan/sheng5.png'
					})
					uni.setTabBarStyle({
						color: '#FFFFFF',
						selectedColor: '#ffd479',
						backgroundColor: '#B32728',
						borderStyle: 'white'
					})
				}
			})

		},
		onHide() {
			uni.setStorageSync('fullscreenShow', false)
		},
		onLoad() {

		},
		methods: {
			isMobile() {
				let flag = navigator.userAgent.match(
					/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
				);
				return flag;
			},
			getopenid_data(data) {
				getOpenid(data).then(response => {
					const data = response.data
					console.log(data)
					// alert(JSON.stringify(response))
					this.token = data.token
					alert(this.token)
					store.state.token = this.token
					if (this.$route.path == '/') {
						this.$router.push({
							name: ''
						})
					} else {
						this.$router.push({
							name: ''
						})
					}
				}).catch(error => {
					alert(error)
				})
			},
			getUrlParam(name) {
				var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
				let url = window.location.href.split('#')[0]
				let search = url.split('?')[1]
				if (search) {
					var r = search.substr(0).match(reg)
					if (r !== null) return unescape(r[2])
					return null
				} else {
					return null
				}
			},
			...mapMutations(['login']),
			initWebsocket() {
				let websocketTimer = null;
				uni.connectSocket({
					url: websocketUrl
				});
				uni.onSocketOpen(() => {
					uni.setStorageSync('socketOpen', true);
					this.websocketSend(JSON.stringify({
						"route": "site.login",
						"token": uni.getStorageSync('accessToken')
					}));
					websocketTimer = setInterval(() => {
						this.websocketSend(JSON.stringify({
							"route": "site.ping"
						}));
					}, 60 * 1000);
					console.log('websocket 连接成功')
				});
				uni.onSocketError(function(res) {
					clearInterval(websocketTimer);
					uni.connectSocket({
						url: websocketUrl
					});
					console.log('WebSocket连接打开失败，请检查！', res);
				});
			},
			websocketSend(data) {
				uni.sendSocketMessage({
					data
				});
			},
		}
	}
</script>

<style lang='scss'>
	@import "@/common/colorui/main.css";
	@import "@/common/colorui/icon.css";
	@import "@/common/colorui/animation.css";

	@import '@/common/css/iconfont/iconfont.css';
	@import '@/common/css/reset.scss';
	@import '@/common/css/uni.scss';
</style>
